# Copyright (C) 2007 Samuel Abels, http://debain.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
import copy
from Scope        import Scope
from TokenList    import TokenList
from Variable     import Variable
from FunctionCall import FunctionCall
from String       import String

class GrammarBody(Scope):
    def __init__(self, parser, parent, inherit = None):
        Scope.__init__(self, 'GrammarBody', parser, parent)
        self.grammar_before = []
        self.grammar        = []
        self.grammar_after  = []

        if inherit is not None:
            self.grammar_before = inherit.grammar_before[:]
            self.grammar        = inherit.grammar[:]
            self.grammar_after  = inherit.grammar_after[:]
        current_grammar = self.grammar

        while parser.next_if('whitespace') or parser.next_if('newline'):
            pass

        # Expect a term.
        while not parser.current_is('close_curly_bracket'):
            if parser.next_if('whitespace'):
                continue
            elif parser.next_if('newline'):
                continue
            elif parser.next_if('comment'):
                parser.expect(self, 'newline')
                continue
            elif parser.next_if('open_edge_bracket'):
                type, token = parser.token()
                parser.expect(self, 'varname')
                if token == 'before':
                    self.grammar_before = []
                    current_grammar     = self.grammar_before
                elif token == 'body':
                    self.grammar    = []
                    current_grammar = self.grammar
                elif token == 'after':
                    self.grammar_after = []
                    current_grammar    = self.grammar_after
                else:
                    msg = 'Unexpected section (%s)' % repr(token)
                    parent.syntax_error(self, msg)
                parser.expect(self, 'close_edge_bracket')
            elif parser.current_is('open_function_call'):
                current_grammar.append(FunctionCall(parser, self))
                parser.expect(self, 'semicolon')
            elif parser.current_is('varname') \
              or parser.current_is('regex_delimiter'):
                current_grammar.append(TokenList(parser, self))
            else:
                self.char = parser.current_char
                self.unexpected_token_error(self, parser.token())

        self.mark_end(parser)


    def dump(self, indent = 0):
        print (' ' * indent) + self.name, repr(self.input)
        for item in self.grammar:
            item.dump(indent + 1)
